<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <el-breadcrumb :separator-icon="ArrowRight" class="bread">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <transition name="fade-right" mode="out-in">
          <el-breadcrumb-item :key="topCategory.id">{{topCategory.name}}</el-breadcrumb-item>
        </transition>
      </el-breadcrumb>
      <!-- 轮播图 -->
      <Carousel :sliders="sliders" style="height:500px" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="sub in topCategory.children" :key="sub.id">
            <a href="javascript:;">
              <img :src="sub.picture" >
              <p>{{sub.name}}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in subList" :key="item.id">
        <div class="head">
          <h3>- {{item.name}} -</h3>
          <p class="tag">{{item.desc}}</p>
          <More />
        </div>
        <div class="body">
          <GoodsItem v-for="g in item.goods" :key="g.id" :goods="g" />
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import GoodsItem from './goods-item'
import Carousel from '@/components/carousel'
import More from '@/components/more'
import { findBanner } from '@/api/home';
import { findTopCategory } from '@/api/category'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed, ref, watch } from 'vue'
import { ArrowRight } from '@element-plus/icons-vue'

  // 轮播图
  const sliders = ref([])
  findBanner().then(data => {
    sliders.value = data.result
  })
   // 面包屑+所有分类
  const store = useStore()
  const route = useRoute()
  const topCategory = computed(() => {
    let cate = {}
    const item = store.state.category.list.find(item => {
      return item.id == route.params.id
    })
    if (item) cate = item
    return cate
  })
  // 推荐商品
  const subList = ref([])
  const getSubList = () => {
    findTopCategory(route.params.id).then(data => {
      subList.value = data.result.children
    })
  }
  watch(() => route.params.id, (newVal) => {
    if (newVal && `/category/${newVal}` === route.path) getSubList()
  }, { immediate: true })

</script>
<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @mainColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
  .bread{
  display: flex;
  padding: 25px 10px;
  // ul li:last-child {}
  // 先找到父元素，找到所有的子元素，找到最后一个，判断是不是LI，是就是选中，不是就是无效选择器
  // ul li:last-of-type {}
  // 先找到父元素，找到所有的类型为li的元素，选中最后一个
  &-item {
    a {
      color: #666;
      transition: all .4s;
      &:hover {
        color: @mainColor;
      }
    }
  }
  i {
    font-size: 12px;
    margin-left: 5px;
    margin-right: 5px;
    line-height: 22px;
    // 样式的方式，不合理
    // &:last-child {
    //   display: none;
    // }
  }
  }
}

</style>